package com.ly.friend.rabbit;


import com.ly.api.domain.dto.JudgeSubmitDTO;
import com.ly.common.core.constants.RabbitMQConstants;
import com.ly.common.core.enums.ResultCode;
import com.ly.common.security.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
//@Component：将该类标记为 Spring 组件，由 Spring 容器管理
@Slf4j
public class JudgeProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void produceMsg(JudgeSubmitDTO judgeSubmitDTO) {
        try {
//            使用 RabbitTemplate 向 RabbitMQ
//            发送消息消息内容是 JudgeSubmitDTO（判题提交数据传输对象）
//            发送到名为 OJ_WORK_QUEUE 的队列
            rabbitTemplate.convertAndSend(RabbitMQConstants.OJ_WORK_QUEUE, judgeSubmitDTO);
        } catch (Exception e) {
            log.error("生产者发送消息异常", e);
            throw new ServiceException(ResultCode.FAILED_RABBIT_PRODUCE);
        }
    }
}
